package com.wangkang.eduservice.controller.front;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangkang.commonutils.R;
import com.wangkang.eduservice.entity.EduCourse;
import com.wangkang.eduservice.entity.EduTeacher;
import com.wangkang.eduservice.service.EduCourseService;
import com.wangkang.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


@Api(tags = "讲师C端服务")
@RestController
@RequestMapping("/eduservice/teacherFront")
//@CrossOrigin
public class TeacherFrontController {

    @Autowired
    private  EduTeacherService teacherService;
    @Autowired
    private  EduCourseService courseService;

    public TeacherFrontController(EduTeacherService teacherService, EduCourseService courseService) {
        this.teacherService = teacherService;
        this.courseService = courseService;
    }


    @ApiOperation("讲师C端分页查询")
    //Swagger中参数page代表当前页码，size代表每页容量。
    @GetMapping("/getTeacherInfoList/{page}/{size}")
    public R getTeacherFrontList(@PathVariable long page ,@PathVariable long size){

        Page<EduTeacher> teacherPage = new Page<>(page,size);
        Map<String,Object> map=teacherService.getTeacherFrontList(teacherPage);
        //teacherPage = teacherService.getTeacherFrontList(teacherPage);
        //不能仅返回total与records,需返回分页所有数据
        //return R.ok().data("page",teacherPage);
        return R.ok().data(map);
    }

    @ApiOperation("讲师详情的功能")
    @GetMapping("/getTeacherFrontInfo/{teacherId}")
    public R getTeacherFront(@PathVariable String teacherId){

        //1.根据讲师id查询讲师的基本信息
        EduTeacher teacher = teacherService.getById(teacherId);

        //2.根据讲师查询所讲课程
        LambdaQueryWrapper<EduCourse> courseQuery = new LambdaQueryWrapper<>();
        //根据course表中的teacher_id字段，作为查询条件，通过list方法去查询出EduCourse课程对象。
        courseQuery.eq(EduCourse::getTeacherId,teacherId);
        List<EduCourse> courseList = courseService.list(courseQuery);
        return R.ok().data("teacher",teacher).data("courseList",courseList);
    }

}